/*
 * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
 * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
 */
package org.sbbs.demo.manager.impl;

import jakarta.annotation.PostConstruct;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import net.oschina.j2cache.CacheChannel;
import org.sbbs.base.jpa.manager.impl.BaseManagerImpl;
import org.sbbs.demo.dao.DemoBeanRepository;
import org.sbbs.demo.entity.DemoBean;
import org.sbbs.demo.manager.DemoBeanManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import static org.sbbs.demo.manager.impl.J2CacheDemoBeanManagerImpl.CACHE_NAME;

/**
 *
 * @author jenkins
 */
@Service
@Qualifier("j2CacheDemoBeanManager")
@CacheConfig(cacheNames = CACHE_NAME)
@Slf4j
public class J2CacheDemoBeanManagerImpl extends BaseManagerImpl<DemoBean, Long,DemoBeanRepository> implements DemoBeanManager {

    protected static final String CACHE_NAME = "DemoBean";

    @Resource
    private CacheChannel cacheChannel;

    /**
     * Spring CacheManager依赖
     */
    @Resource
    private CacheManager cacheManager;
    @Autowired
    private DemoBeanRepository demoBeanRepository;

    @PostConstruct
    public void init() {
        super.setRepo(demoBeanRepository);
    }

    @CacheEvict(key = "#id")
    @Override
    public boolean removeById(Long id) {
        return super.removeById(id); // Generated from nbfs://nbhost/SystemFileSystem/Templates/Classes/Code/OverriddenMethodBody
    }

    @Cacheable(key = "#id")
    @Override
    public DemoBean findById(Long id) {
        return super.findById(id); // Generated from nbfs://nbhost/SystemFileSystem/Templates/Classes/Code/OverriddenMethodBody
    }

    @CachePut(key = "#entity.id",unless = "#result.id == null")
    @Override
    public DemoBean save(DemoBean entity) {
        return super.save(entity); // Generated from nbfs://nbhost/SystemFileSystem/Templates/Classes/Code/OverriddenMethodBody
    }

}
